package com.itboye.eweilisten.me.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.DataCleanManager;

import java.io.File;

/**
 * 设置
 */
public class SetUpActivity extends BaseActivity {
    ImageView back;
    TextView tvTitle;
    RelativeLayout setup_huancun;
    TextView setup_fankui, setup_xieyi, setup_guanyu;//反馈，协议，关于
    private long db, sp, cache, dbmanager;
    private String str1;
    private TextView tvcler;

    private String TAG="SetUpActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_up);
        onClerDBMager();
        tvTitle.setText(R.string.set_up);
    }
    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.setup_fankui:
                intent = new Intent(SetUpActivity.this, WebActivity.class);//FeedBackActivity
                intent.putExtra("title","反馈建议");
                intent.putExtra("url", Const.WEB+"web/feedback?uid="+(String) SPUtils.get(MyApplication.instance, null, Const.ID, ""));
                //intent = new Intent(SetUpActivity.this, TiMuActivity.class);
                startActivity(intent);
                break;
            case R.id.setup_xieyi:
                intent = new Intent(SetUpActivity.this, WebActivity.class);
                intent.putExtra("title","用户协议");
                intent.putExtra("url", Const.WEB+"web/agreement");
                startActivity(intent);
                break;
            case R.id.setup_guanyu:
                intent = new Intent(SetUpActivity.this, WebActivity.class);
                intent.putExtra("title","关于");
                intent.putExtra("url", Const.WEB+"web/about");
                startActivity(intent);
                break;
            case R.id.tvcler:
                if (!TextUtils.isEmpty(tvcler.getText().toString())
                        && !tvcler.getText().toString().equals(".00B")) {
                    new Thread(new MyThread()).start();

                } else {
//                    cache_rel.setClickable(false);
                }
                break;
        }
    }

    public void onClerDBMager() {

        try {
            db = DataCleanManager.getFolderSize(new File("/data/data/"
                    + this.getPackageName() + "/databases"));
            cache = DataCleanManager.getFolderSize(new File("/data/data/"
                    + this.getPackageName() + "/cache"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            sp = DataCleanManager.getFolderSize(new File("/data/data/"
                    + this.getPackageName() + "/shared_prefs"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            long datamanager = db + sp + cache;
            str1 = DataCleanManager.getTotalCacheSize(getApplicationContext(),
                    datamanager);

        } catch (Exception e) {

            e.printStackTrace();
        }
        // DataCleanManager.getFolderSize(cache + eCache + file);

        if (str1 != null && !str1.equals(".00B")) {
            tvcler.setText(getString(R.string.huanchuan) + ":" + str1);//缓存内容大小
            Log.d(TAG+"第一次有数据",str1);
//            cache_rel.setClickable(true);
        } else {
            tvcler.setText("0");
            Log.d(TAG+"清除后得到",str1);
//            cache_rel.setClickable(false);
        }

    }

    /**
     * 清除所有应用缓存，但不会清除本地缓存——>指保存数据
     */
    private void clearCache() {
        Log.d(TAG+"清除",str1);
        DataCleanManager.cleanInternalCache(this);
        DataCleanManager.cleanExternalCache(this);
        DataCleanManager.cleanFiles(this);
        DataCleanManager.cleanCustomCache(str1);
//         DataCleanManager.cleanApplicationData(getApplicationContext(), str1);
        DataCleanManager.clearAllCache(this);
    }

    class MyThread implements Runnable {

        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);

        }

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    clearCache();
                    tvcler.setText("0.00B");
                    Log.d(TAG+"清除",str1);
                    break;
            }

        }
    };
}
